package com.drpclub.gamebackend.repository;

import com.drpclub.gamebackend.dao.ResourceDao;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;
import java.util.Optional;

/**
 * @author ：drp
 * @date ：Created in 2020/5/8 17:55
 */

public interface ResourceRepository extends JpaRepository<ResourceDao, String> {

    Page<ResourceDao> findAllByTypeIsLikeAndIsValueAndIsPublish(Pageable pageable, String type, int isValue, int isPublish);

    @Query(value = "select count(*) from (select count(*) from resources res group by res.type)", nativeQuery = true)
    int countType();

    Page<ResourceDao> findAllByIsValueAndIsPublish(Pageable pageable, int isValue, int isPublish);

    @Query(value = "select DISTINCT (res.collectionName) from ResourceDao res where res.username = :username")
    List<String> findCollectionNameByUsername(@Param ("username") String username);

    List<ResourceDao> findResourceDaoByCollectionNameAndUsername(String collectionName, String username);

    @Query(value = "select res.username from ResourceDao res where res.id = :id")
    String findUsernameById(@Param ("id") String contentId);

    @Modifying
    @Query(value = "update ResourceDao res set res.isPublish = :isPublish where res.id = :contentId")
    int updatePublishByContentId(@Param("isPublish")int isPublish, @Param("contentId")String contentId);

    @Modifying
    @Query(value = "update ResourceDao res set res.title = :title where res.id = :contentId")
    int updateTitleByContentId(@Param("title")String title, @Param("contentId")String contentId);

    @Modifying
    @Query(value = "update ResourceDao res set res.heat = res.heat + 1 where res.id = :contentId")
    int updateHeatAddByContentId(@Param("contentId") String contentId);
}
